<template>
  <el-row>
    <el-col :span="15">
      <router-link :to="{ name: 'DefaultData' }">
        <img src="@/assets/cfswt_logo.png"
             style=" width: 10.625rem; height: auto">
      </router-link>
    </el-col>
    <el-col :span="6">
      <div class="grid-content bg-purple-light">

      </div>
    </el-col>
    <el-col :span="1">
      <el-popover
          placement="top-start"
          trigger="hover">
        <div slot="reference">
          <el-button icon="el-icon-user" size="mini">{{ user.nickName }}</el-button>
        </div>
        <div style="display: flex; align-items: center; justify-content: space-between;">
          <el-link href="registry">注册</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link href="login">登陆</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link @click="logout">登出</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-link @click="modifyDialog">修改密码</el-link>
        </div>
      </el-popover>
    </el-col>
    <el-col :span="1">
      <el-badge :value="reminderCount" class="item" type="primary">
        <el-button icon="el-icon-bell" size="mini" @click="showChatPanel">消息</el-button>
      </el-badge>
    </el-col>

    <el-dialog title="修改密码" width="40%" :visible.sync="modifyPasswordVisible" :center="true"
               :close-on-click-modal="false">
      <el-form :model="passwordForm" size="small" label-width="120px">
        <el-form-item label="原密码">
          <el-input v-model="passwordForm.oldPassword" autocomplete="off" :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="passwordForm.newPasswordTemp" autocomplete="off" :clearable="true"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input v-model="passwordForm.newPassword" autocomplete="off" @input="debouncedCheckPassword"
                    :clearable="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="modifyPasswordVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyPassword">确 定</el-button>
      </div>
    </el-dialog>
  </el-row>
</template>

<script>
import {mapState} from 'vuex';

export default {
  name: "Header",
  data() {
    return {
      modifyPasswordVisible: false,
      user: localStorage.user ? JSON.parse(localStorage.user) : "",
      passwordForm: {
        oldPassword: '',
        newPasswordTemp: '',
        newPassword: ''
      }
    }
  },
  computed: {
    ...mapState({
      reminderCount: state => state.reminderCount,
    })
  },
  methods: {
    logout() {
      this.$http.get("users/logout").then((response) => {
        localStorage.clear();
        location.href = "/";
      })
    },
    validate() {
      if (this.passwordForm.newPassword != this.passwordForm.newPasswordTemp) {
        this.$message.error("两次密码不一致，请确认")
      }
    },
    modifyDialog() {
      this.modifyPasswordVisible = true
    },
    modifyPassword() {
      const passwordData = new URLSearchParams()
      passwordData.append('oldPassword', this.passwordForm.oldPassword)
      passwordData.append('newPassword', this.passwordForm.newPassword)
      this.$http.post("users/modify/password", passwordData).then((response) => {
        if (response.data.code == 1) {
          this.$message({
            showClose: true,
            message: '密码修改成功',
            type: 'success'
          });
          this.modifyPasswordVisible = false;
        } else {
          this.$message.error(response.data.message);
        }
      })
    },
    showChatPanel() {
      this.$bus.$emit('showChatPanel')
    }
  },
  created() {
    this.debouncedCheckPassword = _.debounce(this.validate, 500);
  }
}
</script>
<style scoped>
::v-deep .el-link{
  font-size: 0.75rem !important;
}
</style>